using System;
using System.Collections.Generic;
using CGF;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
using DG.Tweening;
using Org.BouncyCastle.Math.EC;

public class TabDevicesCtrlCell : MonoBehaviour
{
    public Image sliderForegound;
    public Text logText;

    private Vector2 progress = new Vector2(0.6f, 0.9f);
    private float logTimer;
    private List<string> lines = new List<string>();
    private int maxLine = 8;
    private bool isRunning;
    private bool isSliderRunning;
    private bool isPreparing;
    private Tween sliderTween;
    private DateTime createTime;
    private bool isFlag;

    //TODO 替换日志
    private static string[] logs =
    {
        "/v1/ai/queue/R9HT30CHRXV?second=16",
        "Current task 4 cmd: 4",
        "icon: Estimated 1 minute",
        "name: Go to Profile",
        "name_en: Enter Personal Center",
        "participants_count: 0",
        "Duration 16",
        "/v1/ai/queue/R9HT30CHRXV?second=7",
        "Current task 8 cmd: 8",
        "icon: Estimated 1 minute",
        "name: Respond to unread messages",
        "name_en: Reply to unread messages",
        "participants_count: 0",
        "Duration 7",
        "/v1/ai/queue/R9HT30CHRXV?second=16",
        "Current task 1 cmd: 1",
        "icon: Estimated 1 minute",
        "name: Enter Discover",
        "name_en: Enter Explore and Discover",
        "participants_count: 0",
        "Duration 16",
        "/v1/ai/queue/R9HT30CHRXV?second=7",
        "Current task 5 cmd: 5",
        "icon: Estimated 1 minute",
        "name: Next",
        "name_en: Swipe to next",
        "participants_count: 0",
        "name: Next",
        "name_en: Swipe to next",
        "participants_count: 0",
        "Duration 7",
    };

    public void SetRunning(int type, bool isRunning, bool isPreparing, DateTime createTime)
    {
        switch (type)
        {
            case 1:
                progress = new Vector2(0.1f, 0.4f);
                break;
            case 2:
                progress = new Vector2(0.5f, 0.8f);
                break;
            case 3:
                progress = new Vector2(0.7f, 1f);
                break;
        }
        if (isPreparing)
        {
            progress = new Vector2(0.0f, 0.1f);
        }
        this.isRunning = isRunning;
        this.isPreparing = isPreparing;
        this.createTime = createTime;
        if (isRunning)
        {
            if (!isSliderRunning)
            {
                SliderMove(0);
            }
        }
        else
        {
            logText.text = "";
            sliderTween.Kill();
            sliderForegound.fillAmount = 0;
        }
    }

    private void SliderMove(float start)
    {
        isSliderRunning = true;
        float end = Random.Range(progress.x, progress.y);
        float duration = Random.Range(0.5f, 1f);
        sliderTween = DOVirtual.Float(start, end, duration, value =>
        {
            sliderForegound.fillAmount = value;
        }).OnComplete(() =>
        {
            if (isRunning)
            {
                SliderMove(end);
            }
        });
    }

    void Update()
    {
        if (isRunning && logTimer < Time.time)
        {
            if (isPreparing)
            {
                logTimer = Time.time + 0.5f;
                if (logText.text == "preparing")
                {
                    logText.text = "preparing...";
                }
                else if (logText.text == "preparing...")
                {
                    logText.text = "preparing......";
                }
                else
                {
                    logText.text = "preparing";
                }
                if ((DateTime.Now - createTime).TotalMinutes > Config.PreparingMinutes)
                {
                    isPreparing = false;
                    gameObject.Get<FlashLight>("GreenLight").SetColor(false);
                    gameObject.Get<Text>("TextState").text = CommAssets.instance.I18N_running;
                }
            }
            else
            {
                logTimer = Time.time + Random.Range(0.1f, 0.5f);
                AddLog();
            }
        }
    }

    private void AddLog()
    {
        string log = logs[Random.Range(0, logs.Length)];
        lines.Add(log);
        if (lines.Count > maxLine)
        {
            lines.RemoveAt(0);
        }
        logText.text = "";
        for (int i = 0; i < lines.Count; i++)
        {
            if (logText.text != "")
            {
                logText.text += "\n";
            }
            logText.text += lines[i];
        }
        if (isFlag)
        {
            logText.text += "\n...";
        }
        else
        {
            logText.text += "\n......";
        }
        isFlag = !isFlag;
    }
}
